package com.sunjy.common.utils.Stream;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import java.io.*;

/**
 *
 * @author snowwolf-louis
 * @date 17/11/17
 */
public class BodyStreamWrapper {

    private byte[] body;

    public BodyStreamWrapper(InputStream in) throws IOException{
        body = inputStream2byte(in);
    }

    public BodyStreamWrapper(File file) throws IOException{
        body = file2byte(file);
    }

    public BodyStreamWrapper(byte[] body){
        this.body = body;
    }

    public static byte[] inputStream2byte(InputStream is) throws IOException{
        byte[] bytes = null;
        if (is != null) {
            int length = (int) is.available();
            // 当文件的长度超过了int的最大值
            if (length > Integer.MAX_VALUE)
            {
                System.out.println("this file is max ");
                return null;
            }
            bytes = new byte[length];
            int offset = 0;
            int numRead = 0;
            while (offset < bytes.length&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
                offset += numRead;
            }
            // 如果得到的字节长度和file实际的长度不一致就可能出错了
            if (offset < bytes.length) {
                System.out.println("file length is error");
                return null;
            }
            is.close();
        }
        return bytes;
    }

    public static byte[] file2byte(File file) throws IOException {
        byte[] bytes = null;
        if (file != null) {
            InputStream is = new FileInputStream(file);
            int length = (int) file.length();
            if (length > Integer.MAX_VALUE) // 当文件的长度超过了int的最大值
            {
                System.out.println("this file is max ");
                return null;
            }
            bytes = new byte[length];
            int offset = 0;
            int numRead = 0;
            while (offset < bytes.length&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
                offset += numRead;
            }
            // 如果得到的字节长度和file实际的长度不一致就可能出错了
            if (offset < bytes.length) {
                System.out.println("file length is error");
                return null;
            }
            is.close();
        }
        return bytes;
    }


    public byte[] getBody(){
        return body;
    }

    public ServletInputStream getInputStream() throws IOException {

        final ByteArrayInputStream bais = new ByteArrayInputStream(body);

        return new ServletInputStream() {

            @Override
            public int read() throws IOException {
                return bais.read();
            }

            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {

            }
        };
    }
}
